/*
 * BlockingQueue.h
 *
 *  Created on: Nov 4, 2012
 *      Author: lucas
 */

#ifndef BLOCKINGQUEUE_H_
#define BLOCKINGQUEUE_H_
#include "Mutex.h"
#include "ConditionVariable.h"
#include <deque>
#include "Lock.h"
#include <iostream>
template <typename T>
class BlockingQueue {
private:
	Mutex m;
	ConditionVariable cv;
	std::deque<T> c;
public:
	BlockingQueue() : cv(m) {

	}

	void push(const T& data) {
		Lock l(m);
		c.push_back(data);
		std::cout << "Push" << std::endl;
		cv.signal();
	}

	T pop() {
		T ret;
		Lock l(m);
		if (c.empty())
			cv.wait();
		ret = c.front();
		c.pop_front();
		std::cout<< "Pop" << std::endl;
		return ret;
	}

	bool empty() {
		Lock l(m);
		return c.empty();
	}


	~BlockingQueue() {
	}

};

#endif /* BLOCKINGQUEUE_H_ */
